import Vue from 'vue'
import Storage from './store'
import small from '@/smallapp/small'
const jumpPath = (that, path, data) => {
  let baseData={
    sid:'',
    pid:''
  }
  
  baseData.sid=small.globalData.shopId
  baseData.pid=small.wxCache('global_login_info') ? small.wxCache('global_login_info').uid : '0'
  
  const query = Object.assign({}, data)
  // const query = Object.assign({}, baseData, data)
  
  // 小程序跳转
  let _params = ''
  for (let key in query) {
    _params += key + '=' + query[key] + '&'
  }
  let _path = path.indexOf('?') > -1 ? (path + '&') : (path + '?')
  // 如果path没有带/则加上/
  _path[0] == '/' ? '' : _path = '/' + _path
  // console.log('jumpPath',_path,_params)
  window.open(_path + _params)
  
}
const jumpName = (that, name, data) => {
  let baseData={
    sid:'',
    pid:''
  }

  baseData.sid=small.globalData.shopId
  baseData.pid=small.wxCache('global_login_info') ? small.wxCache('global_login_info').uid : '0'
  
  const query = Object.assign({}, baseData, data)
  
  // 小程序跳转
  let _params = ''
  for (let key in query) {
    _params += key + '=' + query[key] + '&'
  }
  // console.log('jump',name,_params)
  window.open('/' + name + '?' + _params)
  
}

const jumpHref = (that, path, data) => {
  const baseData = {
    sid: that.$store.getters.sid || Storage.fetchLocal('sid'),
    pid: that.$store.getters.pid || Storage.fetchLocal('pid')
  }
  const query = Object.assign({}, baseData, data)
  // that.$router.push({ name, query })
  const queryArr = []
  for (const item in query) {
    const str = `${item}=${query[item]}`
    queryArr.push(str)
  }
  const queryStr = queryArr.join('&')
  const url = `${path}?${queryStr}`
  location.href = url
}

Vue.prototype.$JumpPath = jumpPath
Vue.prototype.$JumpName = jumpName
Vue.prototype.$JumpHref = jumpHref